package code.analyze
{
	import code.consts.ConfigConst;
	
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinDll.core.Config;
	
	import flash.utils.Dictionary;
	
	import module.building.model.dataObj.BuildingObj;
	import module.building.model.dataObj.EquipObj;
	import module.building.model.dataObj.OrderObj;
	import module.edittroop.model.dataObj.ArmsConfigObj;
	import module.edittroop.model.dataObj.HeroConfigObj;
	import module.edittroop.model.dataObj.SkillConfigObj;
	import module.shop.model.dataObj.CommodityObj;
	
	/**
	 * 配置表原始数据解析器 
	 * @author Roalnd
	 * 
	 */
	public class ConfigAnalyze
	{
		public function ConfigAnalyze()
		{
			
		}
		
		/**
		 *	解析配置数据
		 * @param name
		 * @return 
		 * 
		 */
		public static function analyze(name:String):Array
		{
			switch(name)
			{
				case ConfigConst.BUILDING:
					return analyzeBuilding();
					break;
				case ConfigConst.BUILDINGLEVEL:
					return analyzeBuildingLevel();
					break;
				case ConfigConst.COMMODITY:
					return analyzeCommodity();
					break;
				case ConfigConst.ORDER:
					return analyzeOrder();
					break;
				case ConfigConst.HERO:
					return anayzeHero();
					break;
				case ConfigConst.ARMS:
					return armsConfigArr();
					break;
				case ConfigConst.SKILLLIST:
					return getSkillConfig();
					break;
				default://默认返回原始数据
					return Dll.getInstance().getSynPreloadRes(name) as Array;
					break;				
			}
			return null;
		}
		
		/**
		 * 取得配置表中所有建筑的信息  
		 * @param data
		 * @return 建筑信息的数组，一个元素包含以下3套信息：</br>
		 * 
		 */
		private static function analyzeBuilding():Array
		{
			var returnArray:Array=[];
			var buildingArray:Array = Dll.getInstance().getSynPreloadRes("building") as Array;
			var priceArray:Array = analyzeCommodity();
			var levelArray:Array = analyzeBuildingLevel();
			var orderArray:Array = analyzeOrder();
			for each(var b:Object in buildingArray)
			{
				var buildingObj:BuildingObj = BuildingObj.parseData(b);
				
				for each (var c:Object in priceArray)
				{
					if(c.serverId == buildingObj.serverId){
						buildingObj.commodityObj = c as CommodityObj;
						break;
					}
				}
				
				
				var buildingLevelArray:Array=[];
				for(var i:int=0;i<levelArray.length;i++)
				{
					if(levelArray[i].serverId == buildingObj.serverId)
					{
						buildingLevelArray.push(levelArray[i]);
						
//						if(levelArray[i+1].serverId != buildingObj.serverId){break;}//该建筑的等级信息已读取完毕，跳出
						if (buildingLevelArray.length == 6){break;}//若已存满6个等级的信息，则跳出
					}
				}
				buildingObj.levelInfo = buildingLevelArray;
				
				var buildingOrderArray:Array = [];
				for(i=0;i<orderArray.length;i++)
				{
					if(orderArray[i].buildingID == buildingObj.serverId)
					{
						buildingOrderArray.push(orderArray[i]);
//						if(orderArray[i+1].buildingID != buildingObj.serverId){break;}//该建筑的订单信息已读取完毕，跳出
					}
				}
				buildingObj.orderArray = buildingOrderArray;
				
				returnArray.push(buildingObj);
			}
			return returnArray;
		}
		
		/**
		 * 解析建筑等级配置表 
		 * @return 
		 */
		private static function analyzeBuildingLevel():Array
		{
			var data:Array = Dll.getInstance().getSynPreloadRes("buildingLevel");
			var returnArray:Array=[];
			for each(var o:Object in data)
			{
				returnArray.push(o);
			}
			return returnArray;
		}
		
		/**
		 * 解析商品配置表 
		 * @return 
		 */
		private static function analyzeCommodity():Array
		{
			var data:Array = Dll.getInstance().getSynPreloadRes("commodity") as Array;
			var returnArray:Array=[];
			for each(var o:Object in data)
			{
				returnArray.push(CommodityObj.parseData(o));
			}
			return returnArray;
		}
		
		/**
		 * 解析订单配置表 
		 * @return 
		 */
		private static function analyzeOrder():Array
		{
			var data:Array = Dll.getInstance().getSynPreloadRes("order") as Array;
			var returnArray:Array = [];
			for each(var o:Object in data)
			{
				returnArray.push(OrderObj.parseData(o));
			}
			return returnArray;
		}
		
		/**
		 *解析英雄配置表 
		 * @return 
		 * 
		 */		
		private static function anayzeHero():Array
		{
			var heroArray:Array = Dll.getInstance().getSynPreloadRes("hero") as Array;
			var heroConfigArr:Array = [];
			if(!heroConfigArr){
				heroConfigArr = [];
			}
				var len:int = heroArray.length;
				for(var i:int=0;i<len;i++){
					heroConfigArr.push(HeroConfigObj.praseData(heroArray[i]));
				}
			return heroConfigArr;
		}
		/**
		 *解析士兵配置表，转化为强类型数组 
		 * @return 
		 * 
		 */		
		private static var _armsConfigArr:Array;
		private static function  armsConfigArr():Array
		{
			if(!_armsConfigArr)
			{
				var armsConfigArr:Array = Dll.getInstance().getSynPreloadRes("arms") as Array;
				_armsConfigArr = [];
				var len:int = armsConfigArr.length;
				for(var j:int = 0;j<len;j++)
				{
					var armsObj:ArmsConfigObj = ArmsConfigObj.praseData(armsConfigArr[j]);
					var skillStrList:Array = String(armsConfigArr[j].skillList).split(",");
					var skillObjList:Array = [];
					var skillLen:int=skillObjList.length;
					for(var i:int=0;i<len;i++){
						var skillObj:SkillConfigObj = getSkillById(skillObjList[i]);
						skillObjList.push(skillObj);
					}
					armsObj.skillList = skillObjList;
					_armsConfigArr.push(armsObj);
				}
			}
			return _armsConfigArr;
		}
		
		private static function getSkillById(id:*):SkillConfigObj{
			var len:int = getSkillConfig.length;
			for(var i:int=0;i<len;i++){
				var skillObj:SkillConfigObj = getSkillConfig()[i];
				if(skillObj.serverId == String(id)){
					return skillObj;
				}
			}
			return null;
		}
		
		/**
		 *通过skillList数值得到技能 名字
		 * @param skill
		 * @return 
		 * 
		 */		
		private static var _skillConfigArr:Array;
		private static function getSkillConfig():Array
		{
			if(!_skillConfigArr){
				var $arr:Array = Dll.getInstance().getSynPreloadRes("skill") as Array;
				_skillConfigArr = [];
				for(var i:uint = 0;i<$arr.length;i++)
				{
					_skillConfigArr.push(SkillConfigObj.praseData($arr[i]));
				}
			}
			return _skillConfigArr;
		}
	}
}